package com.bysj.money_diary;

import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;



public class MainActivity extends Activity implements OnClickListener, OnGestureListener {

	private Button btnInputInfo,btnQueryInfo,btnSetting,btnChart;
	private Jiyibi inputfragment;
	private Chazhangben queryfragment;
	private Gaishezhi settingfragment;
	private Kantubiao chartfragment;
	public int fragmentflag;
	private FragmentManager fragmentmanager;
	public GestureDetector gesturedector;
	public static final int  FLING_MIN_DISTANCE=40;
	public static final int  FLING_MIN_VELOCITY=0;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initView();
		fragmentmanager = getFragmentManager();
		fragmentflag = 0;
		SetTabSelection(0);				
		gesturedector = new GestureDetector(this,this);
	}

	private void initView() {
		// TODO Auto-generated method stub
		btnInputInfo=(Button)findViewById(R.id.rb_input);
		btnQueryInfo=(Button)findViewById(R.id.rb_query);
		btnChart=(Button)findViewById(R.id.rb_chart);
		btnSetting=(Button)findViewById(R.id.rb_setting);
		
		btnInputInfo.setOnClickListener(this);
		btnQueryInfo.setOnClickListener(this);
		btnChart.setOnClickListener(this);
		btnSetting.setOnClickListener(this);
	}

	

	@Override
	public boolean onDown(MotionEvent e) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void onShowPress(MotionEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public boolean onSingleTapUp(MotionEvent e) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
			float distanceY) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void onLongPress(MotionEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
			float velocityY) {
		if(e1.getX()-e2.getX()>FLING_MIN_DISTANCE  && Math.abs(velocityX)>FLING_MIN_VELOCITY)
		{
			turnToFragment(fragmentflag,"left");
		}else if(e2.getX()-e1.getX()>FLING_MIN_DISTANCE && Math.abs(velocityY)>FLING_MIN_VELOCITY)
			turnToFragment(fragmentflag,"right");
			return false;
	}

	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.rb_input:
			SetTabSelection(0);
			break;
		case R.id.rb_query:
			SetTabSelection(1);
			break;
		case R.id.rb_chart:
			SetTabSelection(2);
			break;
		case R.id.rb_setting:
			SetTabSelection(3);
			break;
		default:
			break;
		}
		
	}

	private void SetTabSelection(int index) {
		// TODO Auto-generated method stub
		clearSeleted();
		fragmentmanager=getFragmentManager();
		FragmentTransaction transaction=fragmentmanager.beginTransaction();
		hideFragment(transaction);
		switch (index){
		case 0:
//			btnInputInfo.setBackgroundResource(R.color.main_buttom_bg);
			if(inputfragment==null){
				inputfragment=new Jiyibi();
				transaction.add(R.id.main_container, inputfragment);
			}else{
				transaction.setCustomAnimations(R.anim.fragement_enter, R.anim.fragment_exit);
				transaction.show(inputfragment);
			}
			break;
		case 1:
//			btnQueryInfo.setBackgroundResource(R.color.main_buttom_bg);
			if(queryfragment==null){
				transaction.setCustomAnimations(R.anim.fragement_enter, R.anim.fragment_exit);
				queryfragment=new Chazhangben();
				transaction.add(R.id.main_container, queryfragment);
			}else{
				transaction.show(queryfragment);
			}
			break;
		case 2:
//			btnChart.setBackgroundResource(R.color.main_buttom_bg);
			if(chartfragment==null){
				transaction.setCustomAnimations(R.anim.fragement_enter, R.anim.fragment_exit);
				chartfragment=new Kantubiao();
				transaction.add(R.id.main_container, chartfragment);
			}else{
				transaction.show(chartfragment);
			}
			break;
		case 3:
//			btnSetting.setBackgroundResource(R.color.main_buttom_bg);
			if(settingfragment==null){
				transaction.setCustomAnimations(R.anim.fragement_enter, R.anim.fragment_exit);
				settingfragment=new Gaishezhi();
				transaction.add(R.id.main_container, settingfragment);
			}else{
				transaction.show(settingfragment);
			}
			break;
		}
		transaction.commit();
	}

	private void hideFragment(FragmentTransaction transaction) {
		// TODO Auto-generated method stub
		if(inputfragment!=null)
			transaction.hide(inputfragment);
		if(queryfragment!=null)
			transaction.hide(queryfragment);
		if(chartfragment!=null)
			transaction.hide(chartfragment);
		if(settingfragment!=null)
			transaction.hide(settingfragment);
	}

	private void clearSeleted() {
		// TODO Auto-generated method stub
		btnInputInfo.setBackgroundResource(0);
		btnQueryInfo.setBackgroundResource(0);
		btnChart.setBackgroundResource(0);
		btnSetting.setBackgroundResource(0);
	}
	
	private void setTabSelection(int i,String direction) {
		// TODO Auto-generated method stub
				clearSeleted();
				fragmentmanager=getFragmentManager();
				FragmentTransaction transaction=fragmentmanager.beginTransaction();
				hideFragment(transaction);
					if(i==0)
					{
//						btnInputInfo.setBackgroundResource(R.color.main_buttom_bg);
						if(inputfragment==null)
						{
							inputfragment=new Jiyibi();				
						     transaction.add(R.id.main_container,inputfragment);
						}
						else
						{
							if(direction.endsWith("right"))
								transaction.setCustomAnimations(R.animator.slide_left_in,R.animator.slide_right_out);
							else
								transaction.setCustomAnimations(R.animator.slide_right_in,R.animator.slide_left_out);
							transaction.show(inputfragment);
						}
						  
						  
					}	else if(i==1)
					{
//						btnQueryInfo.setBackgroundResource(R.color.main_buttom_bg);
						if(queryfragment!=null)
						{
							
							if(direction.endsWith("right"))
								transaction.setCustomAnimations(R.animator.slide_left_in,R.animator.slide_right_out);
							else
								transaction.setCustomAnimations(R.animator.slide_right_in,R.animator.slide_left_out);		
							transaction.show(queryfragment);
						}
						else
						{
							queryfragment=new Chazhangben();
						   transaction.add(R.id.main_container,queryfragment);
						  
						}			 
					}else if(i==2)
					{
//						btnChart.setBackgroundResource(R.color.main_buttom_bg);
						if(chartfragment!=null)
						{
							if(direction.endsWith("right"))
								transaction.setCustomAnimations(R.animator.slide_left_in,R.animator.slide_right_out);
							else
								transaction.setCustomAnimations(R.animator.slide_right_in,R.animator.slide_left_out);			
							transaction.show(chartfragment);
						}
						else
						{
							chartfragment=new Kantubiao();
							transaction.add(R.id.main_container,chartfragment);
						}
					}
					else
					{
//						btnSetting.setBackgroundResource(R.color.main_buttom_bg);
						if(settingfragment!=null)
						{
							if(direction.endsWith("right"))
								transaction.setCustomAnimations(R.animator.slide_left_in,R.animator.slide_right_out);
							else
								transaction.setCustomAnimations(R.animator.slide_right_in,R.animator.slide_left_out);			
							transaction.show(settingfragment);
						}
						else
						{
							settingfragment=new Gaishezhi();
							transaction.add(R.id.main_container,settingfragment);
						}
					}
				
				transaction.commit();	
			}
	private void turnToFragment(int flag, String direction) {
		// TODO Auto-generated method stub
		if(direction.equals("right"))
		{
			if(flag==2)
			{					
				setTabSelection(3,"right");
				fragmentflag=3;
			}else if(flag==3)
			{
				setTabSelection(0,"right");
				fragmentflag=0;
			}else if(flag==0)
			{
				setTabSelection(1,"right");
				fragmentflag=1;
			}
		}else if(direction.equals("left"))
		{
			 if(flag==0)
			 {
				 setTabSelection(1,"left");
				 fragmentflag=1;
			 } else if(flag==1){
				 setTabSelection(2,"left");
				 fragmentflag=2;
			 } else if(flag==2)
			 {
				 setTabSelection(3,"left");
				 fragmentflag=3;
			 }
		}
	}
}

